"use strict";

class mongo {
    constructor(ConfigData) {
        this.__libs = require("./lib");
        this.__InstanceRootControl = {
            PluginPoint: {
                gtcpk_be: require("gtcpk-be"),
                mongodb: require("mongodb")
            },
            confirmFinalData: undefined,
            readyStatus: undefined
        };
        try {
            this.__ToolKit = new this.__InstanceRootControl.PluginPoint.gtcpk_be();
        } catch (e) {
            this.__ToolKit = new this.__InstanceRootControl.PluginPoint.gtcpk_be.GeneralToolsCollectionPackageKit();
        }
        this.__ToolKit.DataTypeDetect({
            _GlobalOperate: {
                enableDict: true, bind: {
                    String: {aliasVal: "str", aliasDsc: "为字符串类型"},
                    Array: {aliasVal: "arr", aliasDsc: "为数组类型"},
                    Object: {aliasVal: "obj", aliasDsc: "为对象类型"},
                    Boolean: {aliasVal: "boo", aliasDsc: "为布尔类型"},
                    Number: {aliasVal: "num", aliasDsc: "为数字类型"},
                    Undefined: {aliasVal: "und", aliasDsc: "未知定义类型"},
                    Function: {aliasVal: "fun", aliasDsc: "为函数类型"},
                    AsyncFunction: {aliasVal: "anf", aliasDsc: "为异步函数类型"},
                    Null: {aliasVal: "nul", aliasDsc: "为空数据类型"},
                    process: {aliasVal: "pro", aliasDsc: "Node JS Process对象"},
                    Promise: {aliasVal: "pme", aliasDsc: "Node JS Promise对象"}
                }, RetValueType: 0
            }
        });
        /**数据格式模板*/
        this.OperateTemplate = this.__libs.Tools.OperateTemplate;
        let curIns = this;
        switch (this.__ToolKit.DataTypeDetect(ConfigData)) {
            case "obj":
                this.__libs.Tools.__SpecialTools.ConnectInfoDetect(this, ConfigData).then((val) => {
                    console.log("数据库连接成功！");
                    curIns.__InstanceRootControl.readyStatus = true;
                }).catch((val) => {
                    console.log(`数据库连接测试失败！请核对连接信息体数据！${val}`);
                    curIns.__InstanceRootControl.readyStatus = false;
                });
                break;
            case "und":
                curIns.__InstanceRootControl.readyStatus = false;
                break;
            default:
                console.log("构造数据不是对象！");
                curIns.__InstanceRootControl.readyStatus = false;
                break;
        }
    }

    /**configureConnect，数据库连接配置*/
    async configureConnect(configureConnect) {
        let opStat;
        let opValue;
        await this.__libs.Tools.__SpecialTools.ConnectInfoDetect(this, configureConnect).then((val) => {
            opStat = true;
            opValue = val;
            this.__InstanceRootControl.readyStatus = true;
        }).catch((val) => {
            opStat = false;
            opValue = val;
            this.__InstanceRootControl.readyStatus = false;
        });
        if (opStat) {
            return opValue
        } else {
            throw opValue;
        }
    }

    /**createDataLibrary，创建数据库引擎*/
    async createDataLibrary(dbName) {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let dbOperateStatus;
        let OperateValue;
        let db_FinalName;
        if (typeof dbName === "string" && String(dbName).length > 0) {
            db_FinalName = dbName;
        } else {
            db_FinalName = this.__InstanceRootControl.confirmFinalData.DataBaseName;
        }
        await this.__libs.Engine.createDataBase(this, db_FinalName).then((vl) => {
            dbOperateStatus = true;
            OperateValue = vl;
        }).catch((vl) => {
            dbOperateStatus = false;
            OperateValue = vl;
        });
        if (dbOperateStatus) {
            return OperateValue;
        } else {
            throw OperateValue;
        }
    }

    /**deleteDataLibrary，删除数据库引擎*/
    async deleteDataLibrary(dbName) {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let dbOperateStatus;
        let OperateValue;
        let db_FinalName;
        if (typeof dbName === "string" && String(dbName).length > 0) {
            db_FinalName = dbName;
        } else {
            db_FinalName = this.__InstanceRootControl.confirmFinalData.DataBaseName;
        }
        await this.__libs.Engine.removeDataBase(this, db_FinalName).then((vl) => {
            dbOperateStatus = true;
            OperateValue = vl;
        }).catch((vl) => {
            dbOperateStatus = false;
            OperateValue = vl;
        });
        if (dbOperateStatus) {
            return OperateValue;
        } else {
            throw OperateValue;
        }
    }

    /**createDataCollection，创建数据集合操作引擎*/
    async createDataCollection() {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let CreateOperateStatus;
        let CreateValue;
        let create1 = this.__libs.Tools.OperateTemplate.collectManagerTemplateBuilder();
        switch (arguments.length) {
            case  0:
                create1.DataLibrary = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__InstanceRootControl.confirmFinalData).DataBaseName;
                create1.CollectName = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__InstanceRootControl.confirmFinalData).CollectName;
                break;
            case  1:
                switch (Object.prototype.toString.call(arguments[0])) {
                    case "[object String]":
                        create1.DataLibrary = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__InstanceRootControl.confirmFinalData).DataBaseName;
                        create1.CollectName = arguments[0];
                        break;
                    case "[object Object]":
                        if (this.__libs.Tools.Checker.collectManagerTemplateCheck(arguments[0])) {
                            create1 = this.__libs.Tools.Convert.collectManagerTemplateConvert(arguments[0]);
                        } else {
                            throw new Error("检测到不是有效的数据对象！请使用模板！");
                        }
                        break;
                    default:
                        throw new Error("请传递要创建的集合名称或模板对象数据！");
                }
                break;
            case  2:
                if (typeof arguments[0] !== "string" || typeof arguments[1] !== "string") {
                    throw new Error("传递的两个形参必须是字符串，第一个是要连接的数据库名称，第二个是要创建的集合名称！");
                } else {
                    create1.DataLibrary = arguments[0];
                    create1.CollectName = arguments[1];
                }
                break;
            default:
                throw new Error("参数溢出！");
        }
        await this.__libs.Engine.createCollect(this, create1).then((vl) => {
            CreateOperateStatus = true;
            CreateValue = vl;
        }).catch((vl) => {
            CreateOperateStatus = false;
            CreateValue = vl;
        });
        if (CreateOperateStatus) {
            return CreateValue;
        } else {
            throw CreateValue;
        }
    }

    /**deleteDataCollection，删除数据集合操作引擎*/
    async deleteDataCollection() {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let DeleteOperateStatus;
        let DeleteValue;
        let delete1 = this.__libs.Tools.OperateTemplate.collectManagerTemplateBuilder();
        switch (arguments.length) {
            case  0:
                delete1.DataLibrary = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__InstanceRootControl.confirmFinalData).DataBaseName;
                delete1.CollectName = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__InstanceRootControl.confirmFinalData).CollectName;
                break;
            case  1:
                switch (Object.prototype.toString.call(arguments[0])) {
                    case "[object String]":
                        delete1.DataLibrary = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__InstanceRootControl.confirmFinalData).DataBaseName;
                        delete1.CollectName = arguments[0];
                        break;
                    case "[object Object]":
                        if (this.__libs.Tools.Checker.collectManagerTemplateCheck(arguments[0])) {
                            delete1 = this.__libs.Tools.Convert.collectManagerTemplateConvert(arguments[0]);
                        } else {
                            throw new Error("检测到不是有效的数据对象！请使用模板！");
                        }
                        break;
                    default:
                        throw new Error("请传递要创建的集合名称或模板对象数据！");
                }
                break;
            case  2:
                if (typeof arguments[0] !== "string" || typeof arguments[1] !== "string") {
                    throw new Error("传递的两个形参必须是字符串，第一个是要连接的数据库名称，第二个是要创建的集合名称！");
                } else {
                    delete1.DataLibrary = arguments[0];
                    delete1.CollectName = arguments[1];
                }
                break;
            default:
                throw new Error("参数溢出！");
        }
        await this.__libs.Engine.deleteCollect(this, delete1).then((vl) => {
            DeleteOperateStatus = true;
            DeleteValue = vl;
        }).catch((vl) => {
            DeleteOperateStatus = false;
            DeleteValue = vl;
        });
        if (DeleteOperateStatus) {
            return DeleteValue;
        } else {
            throw DeleteValue;
        }
    }

    /**readerDataOperate，读取数据操作引擎*/
    async readerDataOperate(TemplateDataObject) {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let readerOperateStatus;
        let readerValue;
        let reader1;
        if (this.__libs.Tools.Checker.readerTemplateCheck(this, this.__libs.Tools.Convert.readerTemplateConvert(TemplateDataObject))) {
            reader1 = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__libs.Tools.Convert.readerTemplateConvert(TemplateDataObject));
        } else {
            throw new Error("");
        }
        await this.__libs.Engine.readerData(this, reader1).then((vl) => {
            readerOperateStatus = true;
            readerValue = vl;
        }).catch((vl) => {
            readerOperateStatus = false;
            readerValue = vl;
        });
        if (readerOperateStatus) {
            return readerValue;
        } else {
            throw readerValue;
        }
    }

    /**insertDataOperate，插入数据操作引擎*/
    async insertDataOperate(TemplateDataObject) {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let insertOperateStatus;
        let insertValue;
        let insert1;
        if (this.__libs.Tools.Checker.insertTemplateCheck(this, this.__libs.Tools.Convert.insertTemplateConvert(TemplateDataObject))) {
            insert1 = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__libs.Tools.Convert.insertTemplateConvert(TemplateDataObject));
        } else {
            throw new Error("");
        }
        await this.__libs.Engine.insertData(this, insert1).then((vl) => {
            insertOperateStatus = true;
            insertValue = vl;
        }).catch((vl) => {
            insertOperateStatus = false;
            insertValue = vl;
        });
        if (insertOperateStatus) {
            return insertValue;
        } else {
            throw insertValue;
        }
    }

    /**updateDataOperate，更新数据操作引擎*/
    async updateDataOperate(TemplateDataObject) {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let updateOperateStatus;
        let updateValue;
        let update1;
        if (this.__libs.Tools.Checker.updateTemplateCheck(this, this.__libs.Tools.Convert.updateTemplateConvert(TemplateDataObject))) {
            update1 = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__libs.Tools.Convert.updateTemplateConvert(TemplateDataObject));
        } else {
            throw new Error("");
        }
        await this.__libs.Engine.updateData(this, update1).then((vl) => {
            updateOperateStatus = true;
            updateValue = vl;
        }).catch((vl) => {
            updateOperateStatus = false;
            updateValue = vl;
        });
        if (updateOperateStatus) {
            return updateValue;
        } else {
            throw updateValue;
        }
    }

    /**removeDataOperate，删除数据操作引擎*/
    async removeDataOperate(TemplateDataObject) {
        if (!this.__InstanceRootControl.readyStatus) throw new Error("连接未准备好！");
        let removeOperateStatus;
        let removeValue;
        let remove1;
        if (this.__libs.Tools.Checker.removeTemplateCheck(this, this.__libs.Tools.Convert.removeTemplateConvert(TemplateDataObject))) {
            remove1 = this.__libs.Tools.__SpecialTools.baseAndCollectSelect(this, this.__libs.Tools.Convert.removeTemplateConvert(TemplateDataObject));
        } else {
            throw new Error("");
        }
        await this.__libs.Engine.removeData(this, remove1).then((vl) => {
            removeOperateStatus = true;
            removeValue = vl;
        }).catch((vl) => {
            removeOperateStatus = false;
            removeValue = vl;
        });
        if (removeOperateStatus) {
            return removeValue;
        } else {
            throw removeValue;
        }
    }
}

module.exports = mongo;